﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
    /// <summary>
    /// Summary description for.
    /// Project name: SMS_xoso
    /// channel: n/a
    /// command code: HN X Y
    /// created: 01/09/09 .
    /// Writer : Nampt + Tuanbc
    /// Company: VIT TELECOM
    /// </summary>

    public class XOSO_PL
    {
        #region Declare
        private SMS_MO WMO;
        private SMS_MT WMT;
        //private SMS_LIST WML;
        ArrayList arr = new ArrayList();

        public XOSO_PL(SMS_MO MO)
        {
            WMO = MO;
        }

        public XOSO_PL()
        {
        }
        #endregion
        #region Method
        public void run()
        {           
            WMT = new SMS_MT(WMO);
            string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
            tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
            string[] msg = tmpMsg.Split(new char[] { ' ' });
            string[] strReturnMsg = GetReturnMsg(msg).Split('|');
            for (int i = 0; i < strReturnMsg.Length; i++)
            {
                WMT.AddMessage(WMO.UserID, strReturnMsg[i], SMS_ContentType.SMS_TEXT);
                WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;             
            }
            WMT.Insert();
            return;
        }
        private bool ValidInput(string[] tmp)
        {
            if (tmp.Length <= 0 || tmp.Length > 2) return false;
            return true;
        }
        private bool checkIsNumber(string txNumber)
        {
            bool textIsNumeric = true;
            try
            {
                int.Parse(txNumber.Trim());
            }
            catch
            {
                textIsNumeric = false;
            }
            return textIsNumeric;
        }
        private string GetReturnMsg(string[] arr)
        {
            XOSO_WAIT xs_wait = new XOSO_WAIT();
            string msgBody = "";
            if ((WMO.UserID == "84902222776") || (WMO.UserID == "84978925568") || (WMO.ServiceID.Replace("04", "") == "7562") || (WMO.ServiceID.Replace("04", "") == "7662"))
            {
                string connString = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
                SqlConnection oledbConn = new SqlConnection(connString);
                string strSQl = "";
                if (arr.Length == 0)
                {
                    strSQl = "select numberlucky from xoso_dd where mamien='MB'";
                }
                else if (arr[0] == "TD")
                {
                    strSQl = "select numberlucky from xoso_dd where mamien='MB'";
                }
                else
                {
                    strSQl = "select numberlucky from xoso_dd where matinh='" + arr[0] + "'";
                }
                DataSet ds = new DataSet();
                oledbConn.Open();
                SqlDataAdapter oleda = new SqlDataAdapter(strSQl, oledbConn);
                oleda.Fill(ds, "gets");
                oledbConn.Close();   
                if(ds.Tables["gets"].Rows.Count>0)
                {
                    msgBody = ds.Tables["gets"].Rows[0]["numberlucky"].ToString();
                }
                ds.Dispose();
                oledbConn.Dispose();
                xs_wait.InsetSMS_WAIT(WMO.MO_ID, WMO.UserID, WMO.ServiceID, WMO.CommandCode, "TD", WMO.RequestID, WMO.RequestTime, 0, 1);
            }
            else if ((WMO.UserID == "84978925568") || (WMO.UserID == "84902222776") || (WMO.ServiceID.Replace("04", "") == "6772"))
            {
                string connString = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
                SqlConnection oledbConn = new SqlConnection(connString);
                string strSQl = "";
                if (arr.Length == 0)
                {
                    strSQl = "select menytimes10,numberlucky from xoso_dd where mamien='MB'";
                }
                else if (arr[0] == "TD")
                {
                    strSQl = "select menytimes10,numberlucky from xoso_dd where mamien='MB'";
                }
                else
                {
                    strSQl = "select menytimes10,numberlucky from xoso_dd where matinh='" + arr[0] + "'";
                }
                DataSet ds = new DataSet();
                oledbConn.Open();
                SqlDataAdapter oleda = new SqlDataAdapter(strSQl, oledbConn);
                oleda.Fill(ds, "gets");
                oledbConn.Close();
                if (ds.Tables["gets"].Rows.Count > 0)
                {
                    msgBody = ds.Tables["gets"].Rows[0]["menytimes10"].ToString() + "|" + ds.Tables["gets"].Rows[0]["numberlucky"].ToString();
                }
                ds.Dispose();
                oledbConn.Dispose();
                xs_wait.InsetSMS_WAIT(WMO.MO_ID, WMO.UserID, WMO.ServiceID, WMO.CommandCode, "TD", WMO.RequestID, WMO.RequestTime, 1, 8);
            }
            else
            {
                msgBody = "Du doan cac so phat loc,thong ke chinh xac 100% va ket qua xo so MB moi nhat.Soan tin PL TD gui 7562";
            }           
            return msgBody;
        }
        #endregion
    }
}

